package com.spirng.leetcode.day07;

import java.sql.ClientInfoStatus;
import java.util.ArrayList;
import java.util.List;

public class Test113 {
    public static void main(String[] args) {
        Test113 test113 = new Test113();
        test113.pathSum(new TreeNode(1,new TreeNode(2,new TreeNode(3),null),new TreeNode(2,null,new TreeNode(3))),6);
    }
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        ArrayList<Integer> list = new ArrayList<>();
        ArrayList<List<Integer>> lists = new ArrayList<>();
        if(root==null){
            return lists;
        }
         path(root,targetSum,lists,list);
         return lists;
    }
    public void path(TreeNode node,int sum,List <List<Integer>>lists,List list){
        list.add(node.val);
        if(node.left==null&&node.right==null&&(sum-node.val)==0){
//            list.add(node.val);
            List<Integer> list1 = new ArrayList<>(list);//如果直接赋值两个地址指向一个位置，会导致list修改的时候lists也跟着修改
            lists.add(list1);
        }
        if(node.left!=null){
            path(node.left,sum-node.val,lists,list);
            list.remove(list.size()-1);
        }
        if(node.right!=null) {
            path(node.right, sum - node.val, lists, list);
            list.remove(list.size()-1);
        }
    }
}
